package com.accountant.travelaccountant;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ResultScreen1 extends Activity {

	ListView listView;
	DatabaseOperations_NonActivity databaseOperations;
	ArrayList<String> memberList, toGet, toGive, messages;
	ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		/* calling the super class method */
		super.onCreate(savedInstanceState);
		/* Connecting to the Activity */
		setContentView(R.layout.resultscreen);

		/* Initialising all the array lists */
		memberList = new ArrayList<String>();
		toGet = new ArrayList<String>();
		toGive = new ArrayList<String>();
		messages = new ArrayList<String>();

		/* Create an object of Databaseoperations */
		databaseOperations = new DatabaseOperations_NonActivity(this);

		/* Opening the database */
		databaseOperations.openWriteableDatabase();

		/* Retrieving all the members of the particular account */
		memberList = databaseOperations.retrieveAll(
				DatabaseOperations_NonActivity.getMembertable(),
				new String[] { DatabaseOperations_NonActivity.getMembertablemember() },
				DatabaseOperations_NonActivity.getMembertabletitle() + "=" + "\""
						+ SessionClass_NonActivity.sessionObject().getTitle() + "\"", null,
				null, null, null);

		/* Check if no member in that account */
		if (memberList.size() == 0) {
			Toast.makeText(
					getApplicationContext(),
					"No member is added to the acoount "
							+ SessionClass_NonActivity.sessionObject().getTitle(),
					Toast.LENGTH_SHORT).show();
			finish();
		}

		/*
		 * If only one member is in the account then just tell how much he has
		 * paid
		 */
		if (memberList.size() == 1) {
			messages.add("Only member "
					+ memberList.get(0)
					+ " has paid Rs "
					+ databaseOperations.retrieve(DatabaseOperations_NonActivity
							.getAmounttable(),
							new String[] { DatabaseOperations_NonActivity
									.getAmounttableamount() },
							DatabaseOperations_NonActivity.getAmounttabletitle() + "=\""
									+ SessionClass_NonActivity.sessionObject().getTitle()
									+ "\"", null, null, null, null));
			/* Closing the database */
			databaseOperations.closeDatabase();
		} else {
			toGet = databaseOperations.retrieveAll(
					DatabaseOperations_NonActivity.getMembertable(),
					new String[] { DatabaseOperations_NonActivity.getMembertabletoget() },
					DatabaseOperations_NonActivity.getMembertabletitle() + "=" + "\""
							+ SessionClass_NonActivity.sessionObject().getTitle() + "\"",
					null, null, null, null);
			toGive = databaseOperations.retrieveAll(
					DatabaseOperations_NonActivity.getMembertable(),
					new String[] { DatabaseOperations_NonActivity.getMembertabletogive() },
					DatabaseOperations_NonActivity.getMembertabletitle() + "=" + "\""
							+ SessionClass_NonActivity.sessionObject().getTitle() + "\"",
					null, null, null, null);

			/* Closing the database */
			databaseOperations.closeDatabase();

			messages = MoneyDistribution_NonActivity.FirstFitAlgorithm(memberList, toGet,
					toGive, 0);
			messages.addAll(MoneyDistribution_NonActivity.FirstFitAlgorithm(memberList,
					toGive, toGet, 1));
			while (MoneyDistribution_NonActivity.checkIfAnyPersonStillHasToGiveMoney(toGet,toGive)) {
				messages.addAll(MoneyDistribution_NonActivity.FirstFitAlgorithm(memberList,
						toGet, toGive, 0));
				messages.addAll(MoneyDistribution_NonActivity.FirstFitAlgorithm(memberList,
						toGive, toGet, 1));
			}
			if (messages.size() == 0) {
				messages.add("Account is empty");
			}
		}
		/* Initialising the List View and adding the array list to it */
		listView = (ListView) findViewById(R.id.resultScreen);
		adapter = new ArrayAdapter<String>(getApplicationContext(),
				android.R.layout.simple_list_item_1, messages);
		listView.setAdapter(adapter);
//		listView.setBackgroundColor(Color.BLUE);
	}

}
